home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 August: Tool Chest / Dev.CD Aug 98 TC.toast / Tool Chest / Testing & Debugging / Virtual User / Virtual User Current Release / Examples / Example External Tools / MathTool / MathTool.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-04  |  35.9 KB  |  1,591 lines  |  [TEXT/MPS ]

  1. /*
  2.  *    File:        MathTool.c
  3.  *
  4.  *    Contains:    V.U. 2.1 External Tool Interface Apple Event test program.
  5.  *                Converted from Chuck Lins pascal version
  6.  *
  7.  *                This program receives and responds to Apple Events using the
  8.  *                V.U. 2.1 External Tool interface protocol. Additional commentary
  9.  *                about handling Apple Events in general can be found in the
  10.  *                Inside Macintosh Volume VI.
  11.  *
  12.  *    Written by:    Automation Systems, STM, MSSW
  13.  *
  14.  *    Copyright:    Â© 1992-1994 by Apple Computer, Inc., all rights reserved.
  15.  *
  16.  *    Change History (most recent first):
  17.  *
  18.  *        <10>     9/14/94    CMW        Update for Universal Headers.
  19.  *         <9>     1/27/94    BD        Relstring moved to TextUtils.h from Packages.h.
  20.  *         <8>     1/14/94    CMW        Update for Apple Scriptable external tools interface (V.U. 2.1).
  21.  *         <?>    10/31/92    REM        Added support for new services.
  22.  *                  8/6/92    DGG        Became MathTool.c from original mathTool.c
  23.  *                  8/3/92    DGG        Support for Long Integers added
  24.  *                 7/30/92    DGG        Made to work only with floating point operations.
  25.  *                 7/30/92    DGG        Times service implemented.
  26.  *        <2+>     3/10/92    RV        Use relstring for string comparisons
  27.  *         <2>      3/6/92    JCC        Modifications for new communications methods
  28.  *         <8>     2/20/92    JCC        Ready for SCM build
  29.  *         <7>     2/20/92    JCC        Resource changes
  30.  *         <6>     2/19/92    JCC        Finishing touches
  31.  *         <5>     2/18/92    CMW        Fix a few more bugs with the command name passing.
  32.  *         <4>     2/18/92    JCC        I was returning longs to VU.  NO NO fluffy!
  33.  *         <3>     2/13/92    JCC        Mostly debugged.  Still has a problem with VUCmdHandler though.
  34.  *         <2>     2/12/92    JCC        Compiles.  Not fully debugged yet.
  35.  *
  36.  *    To Do:
  37.  *        
  38.  */
  39.  
  40.                                         //    Includes
  41. #include <Types.h>
  42. #include <Memory.h>
  43. #include <TextUtils.h>
  44. #include <Errors.h>
  45. #include <QuickDraw.h>
  46. #include <Fonts.h>
  47. #include <Dialogs.h>
  48. #include <Windows.h>
  49. #include <Menus.h>
  50. #include <Events.h>
  51. #include <OSEvents.h>
  52. #include <Desk.h>
  53. #include <DiskInit.h>
  54. #include <Resources.h>
  55. #include <ToolUtils.h>
  56. #include <AppleEvents.h>
  57. #include <EPPC.h>
  58. #include <GestaltEqu.h>
  59. #include <PPCToolbox.h>
  60. #include <Processes.h>
  61. #include <String.h>
  62. #include <VUAE.h>
  63. #include <Math.h>
  64.  
  65.                                         //     Constants
  66. #define    kMBarID                    128
  67. #define    kAppleMenu                128
  68. #define    kFileMenu                129
  69. #define    kEditMenu                130
  70. #define NIL                        0L
  71. #define TRUE                    1
  72. #define FALSE                    0
  73. #define    kSuspendResumeMessage    1
  74. #define kMouseMovedMessage        250
  75.  
  76. #define kGREATER                1
  77. #define kLESS                    -1
  78. #define kEQUAL                    0
  79. #define kNotANumber                -1
  80.  
  81.                                         //    MathTool services
  82.  
  83. OSErr    VUExtendedPlusService( AppleEvent msg, AppleEvent reply, long refCon );
  84. OSErr    VUExtendedMinusService( AppleEvent msg, AppleEvent reply, long refCon );
  85. OSErr    VUExtendedTimesService( AppleEvent msg, AppleEvent reply, long refCon );
  86. OSErr    VUExtendedDivideService( AppleEvent msg, AppleEvent reply, long refCon );
  87. OSErr    VUExtendedCompareService( AppleEvent msg, AppleEvent reply, long refCon );
  88. //
  89. OSErr    VUSinService( AppleEvent msg, AppleEvent reply, long refCon );
  90. OSErr    VUCosService( AppleEvent msg, AppleEvent reply, long refCon );
  91. OSErr    VUTanService( AppleEvent msg, AppleEvent reply, long refCon );
  92. OSErr    VUASinService( AppleEvent msg, AppleEvent reply, long refCon );
  93. OSErr    VUACosService( AppleEvent msg, AppleEvent reply, long refCon );
  94. OSErr    VUATanService( AppleEvent msg, AppleEvent reply, long refCon );
  95. //
  96. OSErr    VUSqrtService( AppleEvent msg, AppleEvent reply, long refCon );
  97. OSErr    VUPowerService( AppleEvent msg, AppleEvent reply, long refCon );
  98. OSErr    VULogService( AppleEvent msg, AppleEvent reply, long refCon );
  99. //
  100. OSErr    VULongPlusService( AppleEvent msg, AppleEvent reply, long refCon );
  101. OSErr    VULongMinusService( AppleEvent msg, AppleEvent reply, long refCon );
  102. OSErr    VULongTimesService( AppleEvent msg, AppleEvent reply, long refCon );
  103. OSErr    VULongDivideService( AppleEvent msg, AppleEvent reply, long refCon );
  104. OSErr    VULongModService( AppleEvent msg, AppleEvent reply, long refCon);
  105. OSErr    VULongCompareService( AppleEvent msg, AppleEvent reply, long refCon);
  106.  
  107.                                         // VU required services
  108.                                         
  109. pascal    OSErr  VUServiceHandler( AppleEvent msg, AppleEvent reply, long refCon );
  110.  
  111. OSErr  VUInitializeHandler( AppleEvent msg, AppleEvent reply, long refCon );
  112. OSErr  VUCancelHandler( AppleEvent msg, AppleEvent reply, long refCon );
  113. OSErr  VUSrvcListHandler( AppleEvent msg, AppleEvent reply, long refCon );
  114. OSErr  VUVersionHandler( AppleEvent msg, AppleEvent reply, long refCon );
  115. OSErr  VUHasSrvcHandler( AppleEvent msg, AppleEvent reply, long refCon );
  116. OSErr  VUQuitHandler( AppleEvent msg, AppleEvent reply, long refCon );
  117.  
  118.                                         // AppleEvent required services
  119.                                         
  120. pascal    OSErr  AEOpenHandler( AppleEvent msg, AppleEvent reply, long refCon );
  121. pascal    OSErr  AEOpenDocHandler( AppleEvent msg, AppleEvent reply, long refCon );
  122. pascal    OSErr  AEQuitHandler( AppleEvent msg, AppleEvent reply, long refCon );
  123. pascal    OSErr  AEPrintHandler( AppleEvent msg, AppleEvent reply, long refCon );
  124.  
  125.                                         // Support
  126.                                         
  127. void     InitAEStuff(void);
  128. void     InitProgram(void);
  129. void     Idling(void);
  130. void     InvokeDA (MenuHandle theMenu, long theItem);
  131. void     BuildMenuBar(void);
  132. void     MenuCommand (long theCmd);
  133. void     DispatchMouseDown (EventRecord theEvent);
  134. void     DispatchKeyDown (EventRecord theEvent);
  135. void     ActivateWindow (WindowPtr theWindow, Boolean activating);
  136. void     UpdateWindow (WindowPtr theWindow);
  137. void     DiskInserted (long diskInfo);
  138. void     DispatchOSEvent (EventRecord theEvent);
  139. void     DispatchHighEvent (EventRecord theEvent);
  140. pascal  OSErr  VUErrorReply    (AppleEvent *reply, Str255 errorText, OSErr errorNo);
  141. void     ReportError (OSErr err, long where);
  142. void    SrvcError (Str255 msg);
  143.  
  144.                                         //    Globals
  145.                                         
  146. Boolean            hasAE, quitting, background;
  147. Handle            theMenuBar;
  148. MenuHandle        appleMenu, fileMenu, editMenu;
  149. EventRecord        theEvent;
  150. OSErr            aeError;
  151. Str255            strTemp1, strTemp2;
  152.  
  153.  
  154.                                         //    Service Table definition
  155.                                         
  156. typedef struct {
  157.     char*    serviceName;
  158.     OSErr    (*serviceFunction)( AppleEvent, AppleEvent, long );
  159. } ServiceInfo;
  160.  
  161. ServiceInfo    VU_Tool_Services[] = {
  162.     { "Initialize", VUInitializeHandler },
  163.     { "Cancel", VUCancelHandler },
  164.     { "GetToolServices", VUSrvcListHandler },
  165.     { "GetToolVersion", VUVersionHandler },
  166.     { "ServiceSupported", VUHasSrvcHandler },
  167.     { "Quit", VUQuitHandler },
  168. //
  169.     { "fplus", VUExtendedPlusService },
  170.     { "fminus", VUExtendedMinusService },
  171.     { "ftimes", VUExtendedTimesService },
  172.     { "fdivide", VUExtendedDivideService },
  173.     { "fcompare", VUExtendedCompareService },
  174. //
  175.     { "sin", VUSinService },
  176.     { "cos", VUCosService },
  177.     { "tan", VUTanService },
  178.     { "asin", VUASinService },
  179.     { "acos", VUACosService },
  180.     { "atan", VUATanService },
  181. //
  182.     { "sqrt", VUSqrtService },
  183.     { "power", VUPowerService },
  184.     { "ln", VULogService },
  185. //
  186.     { "lplus", VULongPlusService },
  187.     { "lminus", VULongMinusService },
  188.     { "ltimes", VULongTimesService },
  189.     { "ldivide", VULongDivideService},
  190.     { "lcompare", VULongCompareService },
  191.     { "lmod", VULongModService }
  192. };
  193.     
  194. const long    NumberOfServices = (sizeof( VU_Tool_Services ) / sizeof( ServiceInfo ));
  195.  
  196.  
  197.                                         //    ** Functions **
  198. main()
  199. {
  200. //    Main event loop. Just get events, and dispatch them to an
  201. //    appropriate handler until we get the 'quit' command.
  202.  
  203.     InitProgram();
  204.  
  205.     while (!quitting) {
  206.         WaitNextEvent (everyEvent, &theEvent, 30, NIL);
  207.             switch (theEvent.what) {
  208.                 case nullEvent: 
  209.                     Idling; 
  210.                     break;
  211.                 case mouseDown:
  212.                     DispatchMouseDown(theEvent);
  213.                     break;
  214.                 case keyDown:
  215.                 case autoKey:
  216.                     DispatchKeyDown(theEvent);
  217.                     break;
  218.                 case activateEvt:
  219.                     ActivateWindow((WindowPtr)theEvent.message,
  220.                             (theEvent.modifiers & activeFlag) != 0);
  221.                     break;
  222.                 case updateEvt:
  223.                     UpdateWindow((WindowPtr)theEvent.message);
  224.                     break;
  225.                 case diskEvt:
  226.                     DiskInserted(theEvent.message);
  227.                     break;
  228.                 case osEvt:
  229.                     DispatchOSEvent(theEvent);
  230.                     break;
  231.                 case kHighLevelEvent:
  232.                     DispatchHighEvent(theEvent);
  233.                     break;
  234.             }
  235.     }
  236. }
  237.  
  238.  
  239. OSErr  ExtractStringFromAEList( AEDescList paramList, long index, char* string)
  240. {
  241.     OSErr            aeErr;
  242.     AEKeyword        paramKeyword;
  243.     DescType        actualType;
  244.     Size            actualSize;
  245.  
  246.     aeErr = AEGetNthPtr( ¶mList,
  247.                          index,
  248.                          typeChar,
  249.                          ¶mKeyword,
  250.                          &actualType,
  251.                          (Ptr) string,
  252.                          255,
  253.                          &actualSize );
  254.     if( aeErr )
  255.     {
  256.         ReportError( aeErr, 13 );
  257.     }
  258.     else
  259.     {
  260.         string[actualSize] = '\0';
  261.     }
  262.  
  263.     return( aeErr );
  264. }
  265.  
  266.  
  267. pascal    OSErr  VUServiceHandler(AppleEvent msg, AppleEvent reply, long refcon)
  268. {
  269.     OSErr        aeErr;
  270.     Str255        serviceName = "";
  271.     DescType    actualType;
  272.     Size        actualSize;
  273.     long        i;
  274.     
  275.     // Get the AE Service Name
  276.     aeErr = AEGetParamPtr(&msg,
  277.                            kVUAESrvcName,
  278.                            typeChar,
  279.                            &actualType,
  280.                            serviceName,
  281.                            255,
  282.                            &actualSize);
  283.     ReportError(aeErr, 1);
  284.     if( aeErr == noErr )
  285.     {
  286.         serviceName[ actualSize ] = '\0';
  287.     }
  288.     
  289.     for( i = 0; i < NumberOfServices; i++ )
  290.     {
  291.         if( !relstring( serviceName, VU_Tool_Services[ i ].serviceName, false, true ) )
  292.         {
  293.             aeErr = (*(VU_Tool_Services[ i ].serviceFunction))( msg, reply, refcon );
  294.             return( aeErr );
  295.         }
  296.     }
  297.     
  298.     return( errAEUnknownService);
  299. }
  300.  
  301. OSErr  VUInitializeHandler( AppleEvent /*msg*/, AppleEvent /*reply*/, long /*refCon*/ )
  302. {    
  303.     return noErr;
  304. }
  305.  
  306. OSErr  VUCancelHandler( AppleEvent /*msg*/, AppleEvent /*reply*/, long /*refCon*/ )
  307. {
  308.     return noErr;
  309. }
  310.  
  311. OSErr  VUQuitHandler( AppleEvent /*msg*/, AppleEvent /*reply*/, long /*refCon*/ )
  312. {
  313.     quitting = TRUE;
  314.     return noErr;
  315. }
  316.  
  317. OSErr  VUVersionHandler( AppleEvent, AppleEvent reply, long )
  318. {
  319. //    This Apple Event handler responds to the messages with event class 'v.u.'
  320. //    and event id 'vers'. This AppleEvent asks an external tool to respond
  321. //    with version information about itself.
  322.     
  323. //    The tool name is used by V.U. 2.0 as the name of a tool as shown to a user.
  324. //    Thus, this should be something descriptive; e.g., 'Screen Capture Tool'.
  325.     
  326. //    The version number is simply a number returned to the caller indicating
  327. //    the version of the tool. A V.U. 2.0 script writer could perhaps used this
  328. //    information in verifying that the current release of an external tool was
  329. //    present.
  330.     
  331. //    The version string simply allows more information to be sent by the external
  332. //    tool. V.U. 2.0 does not use the version string.
  333.     
  334. //    This routine simply stuffs values into
  335. //    the Apple Event reply using Apple Event Manager routines.
  336.  
  337.     OSErr        aeErr;
  338.     AEDescList    srvcList;
  339.     char*        strPtr;
  340.  
  341.     aeErr = AECreateList( NIL, 0, FALSE, &srvcList );
  342.     if( aeErr )
  343.     {
  344.         ReportError( aeErr, 3 );
  345.         return( aeErr );
  346.     }
  347.  
  348.     
  349.     strPtr = "MathTool";
  350.     aeErr = AEPutPtr( &srvcList,
  351.                       1,
  352.                       typeChar,
  353.                       strPtr,
  354.                       strlen( strPtr ) );
  355.     if( aeErr )
  356.     {
  357.         ReportError( aeErr, 4 );
  358.     }
  359.     else
  360.     {
  361.         strPtr = "2.0";
  362.         aeErr = AEPutPtr( &srvcList,
  363.                           2,
  364.                           typeChar,
  365.                           strPtr,
  366.                           strlen( strPtr ) );
  367.         if( aeErr )
  368.         {
  369.             ReportError( aeErr, 5 );
  370.         }
  371.         else
  372.         {
  373.             strPtr = "MathTool 2.0 by Automation Systems, Apple Computer, Inc.";
  374.             aeErr = AEPutPtr( &srvcList,
  375.                               3,
  376.                               typeChar,
  377.                               strPtr,
  378.                               strlen( strPtr ) );
  379.             if( aeErr )
  380.             {
  381.                 ReportError( aeErr, 6 );
  382.             }
  383.             else
  384.             {
  385.                 aeErr = AEPutKeyDesc( &reply,
  386.                                       kVUAESrvcResults,
  387.                                       &srvcList );
  388.                 if( aeErr )
  389.                 {
  390.                     ReportError( aeErr, 7 );
  391.                 }
  392.             }
  393.         }
  394.     }
  395.  
  396.     AEDisposeDesc( &srvcList );
  397.     return( aeErr );
  398. }
  399.  
  400.  
  401. OSErr  VUHasSrvcHandler( AppleEvent msg, AppleEvent reply, long )
  402. {
  403. //    This Service asks an external tool whether a
  404. //    service with a specific name is supported by the tool.
  405.     
  406. //    The routine first extracts the service name from the Apple Event. If the
  407. //    service name cannot be extracted for some reason from the Apple Event,
  408. //    a standard error reply message is sent back to V.U. 2.0.
  409.     
  410. //    The routine simply returns the AppleEvent standard types 'typeTrue' or
  411. //    'typeFalse' depending on whether the service name is supported by the
  412. //    tool or not. The Apple Event Manager will coerce these standard types
  413. //    into an Apple Event Boolean value if necessary (and requested by V.U. 2.0).
  414.  
  415.     OSErr        aeErr;
  416.     Str255        serviceName;
  417.     long        i;
  418.  
  419.     AEDescList    paramList;
  420.     long        numParams;
  421.     
  422.     
  423.     aeErr = AEGetParamDesc( &msg,
  424.                             kVUAESrvcParameters,
  425.                             typeAEList,
  426.                             ¶mList );
  427.     if( aeErr )
  428.     {
  429.         ReportError( aeErr, 14 );
  430.         return( aeErr );
  431.     }
  432.     
  433.     if( aeErr = AECountItems( ¶mList, &numParams ) )
  434.     {
  435.         ReportError( aeErr, 15 );
  436.     }
  437.     else if( numParams != 1 )
  438.     {
  439.         if( aeErr = VUErrorReply( &reply,
  440.                                 "Incorrect number of parameters supplied!",
  441.                                 errAEWrongParameters ) )
  442.         {
  443.             ReportError( aeErr, 16 );
  444.         }
  445.         aeErr = errAEWrongParameters;
  446.     }
  447.     else if( aeErr = ExtractStringFromAEList( paramList, 1, serviceName ) )
  448.     {
  449.         ReportError( aeErr, 17 );
  450.     }
  451.     
  452.     AEDisposeDesc( ¶mList );
  453.     
  454.     if( aeErr )
  455.     {
  456.         return( aeErr );
  457.     }
  458.  
  459.     for( i = 0; i < NumberOfServices; i++ )
  460.     {
  461.         if( !relstring( serviceName, VU_Tool_Services[ i ].serviceName, false, true ) )
  462.         {
  463.             aeErr = AEPutParamPtr( &reply,
  464.                                    kVUAESrvcResults,
  465.                                    typeTrue,
  466.                                    NIL,
  467.                                    0);
  468.             return( aeErr );
  469.         }
  470.     }
  471.     
  472.     aeErr = AEPutParamPtr( &reply,
  473.                            kVUAESrvcResults,
  474.                            typeFalse,
  475.                            NIL,
  476.                            0);
  477.     if( aeErr )
  478.     {
  479.         ReportError(aeErr, 9);
  480.         return( aeErr );
  481.     }
  482.  
  483.     return(aeErr);
  484. }
  485.  
  486.  
  487. OSErr  VUSrvcListHandler( AppleEvent, AppleEvent reply, long )
  488. {
  489. //    This Apple Event handler responds to the messages with event class 'v.u.'
  490. //    and event id 'cmdl'. This AppleEvent asks an external tool for a list of
  491. //    all service names supported by the tool.
  492.     
  493. //    Remember that Apple Event Lists are all one (1) based; i.e., indexes
  494. //    start from one (not zero).
  495.     
  496. //    The first step creates an Apple Event List descriptor. This is followed by
  497. //    stuffing each service name into the list, starting at index number one.
  498. //    Once the list if filled with the service names, the list itself is inserted
  499. //    into the Apple Event reply.
  500.  
  501.     OSErr        aeErr;
  502.     AEDescList    srvcList;
  503.     char*        strPtr;
  504.     long        i;
  505.  
  506.     aeErr = AECreateList( NIL, 0, FALSE, &srvcList );
  507.     if( aeErr )
  508.     {
  509.         ReportError( aeErr, 10 );
  510.         return( aeErr );
  511.     }
  512.  
  513.     for( i = 0; i < NumberOfServices; i++ )
  514.     {
  515.         strPtr = VU_Tool_Services[ i ].serviceName;
  516.         aeErr = AEPutPtr( &srvcList,
  517.                           i + 1,
  518.                           typeChar,
  519.                           strPtr,
  520.                           strlen( strPtr ) );
  521.         if( aeErr )
  522.         {
  523.             ReportError( aeErr, 11 );
  524.             AEDisposeDesc( &srvcList );
  525.             return( aeErr );
  526.         }
  527.     }
  528.     
  529.     
  530.     aeErr = AEPutKeyDesc( &reply,
  531.                           kVUAESrvcResults,
  532.                           &srvcList );
  533.     if( aeErr )
  534.     {
  535.         ReportError( aeErr, 12 );
  536.     }
  537.     
  538.     AEDisposeDesc( &srvcList );
  539.  
  540.     return( aeErr );
  541. }
  542.  
  543.  
  544. OSErr  ExtractExtendedFromAEList( AEDescList paramList, long index, extended* result )
  545. {
  546.     OSErr            aeErr;
  547.     AEKeyword        paramKeyword;
  548.     DescType        actualType;
  549.     Size            actualSize;
  550.  
  551.     aeErr = AEGetNthPtr( ¶mList,
  552.                          index,
  553.                          typeExtended,
  554.                          ¶mKeyword,
  555.                          &actualType,
  556.                          (Ptr) result,
  557.                          sizeof( *result ),
  558.                          &actualSize );
  559.     if( aeErr )
  560.     {
  561.         ReportError( aeErr, 13 );
  562.     }
  563.  
  564.     return( aeErr );
  565. }
  566.  
  567. OSErr  ExtractLongIntegerFromAEList( AEDescList paramList, long index, long* result )
  568. {
  569.     OSErr            aeErr;
  570.     AEKeyword        paramKeyword;
  571.     DescType        actualType;
  572.     Size            actualSize;
  573.  
  574.     aeErr = AEGetNthPtr( ¶mList,
  575.                          index,
  576.                          typeLongInteger,
  577.                          ¶mKeyword,
  578.                          &actualType,
  579.                          (Ptr) result,
  580.                          sizeof( *result ),
  581.                          &actualSize );
  582.     if( aeErr )
  583.     {
  584.         ReportError( aeErr, 13 );
  585.     }
  586.  
  587.     return( aeErr );
  588. }
  589.  
  590. // ExtractExtendedPairFromAEList will get the firstNumber and secondNumber with
  591. //    error checking from a given AppleEvent list.
  592. OSErr ExtractExtendedPairFromAEList( extended* firstNumber, extended* secondNumber, 
  593.                                         AppleEvent msg, AppleEvent reply)
  594. {
  595.     OSErr        aeErr;
  596.     AEDescList    paramList;
  597.     long        numParams;
  598.     
  599.     aeErr = AEGetParamDesc( &msg,
  600.                             kVUAESrvcParameters,
  601.                             typeAEList,
  602.                             ¶mList );
  603.     if( aeErr )
  604.     {
  605.         ReportError( aeErr, 14 );
  606.         return( aeErr );
  607.     }
  608.     
  609.     if( aeErr = AECountItems( ¶mList, &numParams ) )
  610.     {
  611.         ReportError( aeErr, 15 );
  612.     }
  613.     else if( numParams != 2 )
  614.     {
  615.         if( aeErr = VUErrorReply( &reply,
  616.                                 "wrong number of parameters",
  617.                                 errAEWrongParameters ) )
  618.         {
  619.             ReportError( aeErr, 16 );
  620.         }
  621.         aeErr = errAEWrongParameters;
  622.     }
  623.     else if( aeErr = ExtractExtendedFromAEList( paramList, 1, firstNumber ) )
  624.     {
  625.         ReportError( aeErr, 17 );
  626.     }
  627.     else if( aeErr = ExtractExtendedFromAEList( paramList, 2, secondNumber ) )
  628.     {
  629.         ReportError( aeErr, 18 );
  630.     }
  631.     
  632.     AEDisposeDesc( ¶mList );
  633.     
  634.     if( aeErr )
  635.     {
  636.         return( aeErr );
  637.     }
  638. }
  639.  
  640. // ExtractExtendedSingleFromAEList will get a single number with
  641. //    error checking from a given AppleEvent list.
  642. OSErr ExtractExtendedSingleFromAEList( extended* theNumber, AppleEvent msg, AppleEvent reply)
  643. {
  644.     OSErr        aeErr;
  645.     AEDescList    paramList;
  646.     long        numParams;
  647.     
  648.     aeErr = AEGetParamDesc( &msg,
  649.                             kVUAESrvcParameters,
  650.                             typeAEList,
  651.                             ¶mList );
  652.     if( aeErr )
  653.     {
  654.         ReportError( aeErr, 14 );
  655.         return( aeErr );
  656.     }
  657.     
  658.     if( aeErr = AECountItems( ¶mList, &numParams ) )
  659.     {
  660.         ReportError( aeErr, 15 );
  661.     }
  662.     else if( numParams != 1 )
  663.     {
  664.         if( aeErr = VUErrorReply( &reply,
  665.                                 "wrong number of parameters",
  666.                                 errAEWrongParameters ) )
  667.         {
  668.             ReportError( aeErr, 16 );
  669.         }
  670.         aeErr = errAEWrongParameters;
  671.     }
  672.     else if( aeErr = ExtractExtendedFromAEList( paramList, 1, theNumber ) )
  673.     {
  674.         ReportError( aeErr, 17 );
  675.     }
  676.     
  677.     AEDisposeDesc( ¶mList );
  678.     
  679.     if( aeErr )
  680.     {
  681.         return( aeErr );
  682.     }
  683. }
  684.  
  685. // ExtractLongIntegerFromAEList will get the firstNumber and secondNumber with
  686. //    error checking from a given AppleEvent list.
  687. OSErr ExtractLongIntegerPairFromAEList( long* firstNumber, long* secondNumber, 
  688.                                         AppleEvent msg, AppleEvent reply)
  689. {
  690.     OSErr        aeErr;
  691.     AEDescList    paramList;
  692.     long        numParams;
  693.     
  694.     aeErr = AEGetParamDesc( &msg,
  695.                             kVUAESrvcParameters,
  696.                             typeAEList,
  697.                             ¶mList );
  698.     if( aeErr )
  699.     {
  700.         ReportError( aeErr, 14 );
  701.         return( aeErr );
  702.     }
  703.     
  704.     if( aeErr = AECountItems( ¶mList, &numParams ) )
  705.     {
  706.         ReportError( aeErr, 15 );
  707.     }
  708.     else if( numParams != 2 )
  709.     {
  710.         if( aeErr = VUErrorReply( &reply,
  711.                                 "wrong number of parameters",
  712.                                 errAEWrongParameters ) )
  713.         {
  714.             ReportError( aeErr, 16 );
  715.         }
  716.         aeErr = errAEWrongParameters;
  717.     }
  718.     else if( aeErr = ExtractLongIntegerFromAEList( paramList, 1, firstNumber ) )
  719.     {
  720.         ReportError( aeErr, 17 );
  721.     }
  722.     else if( aeErr = ExtractLongIntegerFromAEList( paramList, 2, secondNumber ) )
  723.     {
  724.         ReportError( aeErr, 18 );
  725.     }
  726.     
  727.     AEDisposeDesc( ¶mList );
  728.     
  729.     if( aeErr )
  730.     {
  731.         return( aeErr );
  732.     }
  733. }
  734.  
  735. // VUExtendedPlusService will carry out the request to do 
  736. //    addition with floating point numbers.
  737. OSErr  VUExtendedPlusService( AppleEvent msg, AppleEvent reply, long )
  738. {
  739.     OSErr        aeErr;
  740.     extended    leftNumber, rightNumber, result;
  741.     
  742.     
  743.     aeErr = ExtractExtendedPairFromAEList(&leftNumber, &rightNumber, msg, reply);
  744.     if( aeErr )
  745.     {
  746.         return( aeErr );
  747.     }
  748.  
  749.     result = leftNumber + rightNumber;
  750.  
  751.     aeErr = AEPutParamPtr( &reply, kVUAESrvcResults, typeExtended, 
  752.                             (Ptr) &result, sizeof( result ) );
  753.     if( aeErr )
  754.     {
  755.         ReportError( aeErr, 19 );
  756.         return( aeErr );
  757.     }
  758.  
  759.     return( aeErr );
  760. }
  761.  
  762.  
  763. // VUExtendedMinusService will carry out the request to do
  764. //    subtraction with floating point numbers.
  765. OSErr  VUExtendedMinusService( AppleEvent msg, AppleEvent reply, long )
  766. {
  767.     OSErr        aeErr;
  768.     extended    leftNumber, rightNumber, result;
  769.     
  770.     
  771.     aeErr = ExtractExtendedPairFromAEList(&leftNumber, &rightNumber, msg, reply);
  772.     if( aeErr )
  773.     {
  774.         return( aeErr );
  775.     }
  776.  
  777.     result = leftNumber - rightNumber;
  778.  
  779.     aeErr = AEPutParamPtr( &reply, kVUAESrvcResults, typeExtended,
  780.                             (Ptr) &result, sizeof( result ) );
  781.     if( aeErr )
  782.     {
  783.         ReportError( aeErr, 19 );
  784.         return( aeErr );
  785.     }
  786.  
  787.     return( aeErr );
  788. }
  789.  
  790. // The VUExtendedTimesService will carry out the request to do
  791. //    multiplication on floating point numbers.
  792. OSErr  VUExtendedTimesService( AppleEvent msg, AppleEvent reply, long )
  793. {
  794.     OSErr        aeErr;
  795.     extended     leftNumber, rightNumber, result;
  796.     
  797.     
  798.     aeErr = ExtractExtendedPairFromAEList(&leftNumber, &rightNumber, msg, reply);
  799.     if( aeErr )
  800.     {
  801.         return( aeErr );
  802.     }
  803.  
  804.     result = leftNumber * rightNumber;
  805.  
  806.     aeErr = AEPutParamPtr( &reply, kVUAESrvcResults, typeExtended,
  807.                             (Ptr) &result, sizeof( result ) );
  808.     if( aeErr )
  809.     {
  810.         ReportError( aeErr, 19 );
  811.         return( aeErr );
  812.     }
  813.  
  814.     return( aeErr );
  815. }
  816.  
  817. // The VUExtendedDivideService will carry out the request to do
  818. //    division on floating point numbers.
  819. OSErr  VUExtendedDivideService( AppleEvent msg, AppleEvent reply, long )
  820. {
  821.     OSErr        aeErr;
  822.     extended    leftNumber, rightNumber, result;
  823.     
  824.     
  825.     aeErr = ExtractExtendedPairFromAEList(&leftNumber, &rightNumber, msg, reply);
  826.     if( aeErr )
  827.     {
  828.         return( aeErr );
  829.     }
  830.  
  831.     result = leftNumber / rightNumber;
  832.  
  833.     aeErr = AEPutParamPtr( &reply, kVUAESrvcResults, typeExtended,
  834.                             (Ptr) &result, sizeof( result ) );
  835.     if( aeErr )
  836.     {
  837.         ReportError( aeErr, 19 );
  838.         return( aeErr );
  839.     }
  840.  
  841.     return( aeErr );
  842. }
  843.  
  844. // The VUExtendedCompareService will carry out the request to do
  845. //    comparison on floating point numbers.
  846. OSErr  VUExtendedCompareService( AppleEvent msg, AppleEvent reply, long )
  847. {
  848.     OSErr        aeErr;
  849.     extended    leftNumber, rightNumber;
  850.     short        result;
  851.     
  852.     
  853.     aeErr = ExtractExtendedPairFromAEList(&leftNumber, &rightNumber, msg, reply);
  854.     if( aeErr )
  855.     {
  856.         return( aeErr );
  857.     }
  858.  
  859.     if (leftNumber > rightNumber)
  860.         result = kGREATER;
  861.     else if (leftNumber < rightNumber)
  862.         result = kLESS;
  863.     else
  864.         result = kEQUAL;
  865.  
  866.     aeErr = AEPutParamPtr( &reply, kVUAESrvcResults, typeShortInteger,
  867.                             (Ptr) &result, sizeof( result ) );
  868.     if( aeErr )
  869.     {
  870.         ReportError( aeErr, 19 );
  871.         return( aeErr );
  872.     }
  873.  
  874.     return( aeErr );
  875. }
  876.  
  877. // The VUSinService will carry out the request to do
  878. //    sine operation on a given number.
  879. OSErr  VUSinService( AppleEvent msg, AppleEvent reply, long )
  880. {
  881.     OSErr        aeErr;
  882.     extended    theNumber, result;
  883.     
  884.     
  885.     aeErr = ExtractExtendedSingleFromAEList(&theNumber, msg, reply);
  886.     if( aeErr )
  887.     {
  888.         return( aeErr );
  889.     }
  890.  
  891.     result = sin(theNumber);
  892.  
  893.     aeErr = AEPutParamPtr( &reply, kVUAESrvcResults, typeExtended,
  894.                             (Ptr) &result, sizeof( result ) );
  895.     if( aeErr )
  896.     {
  897.         ReportError( aeErr, 19 );
  898.         return( aeErr );
  899.     }
  900.  
  901.     return( aeErr );
  902. }
  903.  
  904. // The VUCosService will carry out the request to do
  905. //    cosine operation on a given number.
  906. OSErr  VUCosService( AppleEvent msg, AppleEvent reply, long )
  907. {
  908.     OSErr        aeErr;
  909.     extended    theNumber, result;
  910.     
  911.     
  912.     aeErr = ExtractExtendedSingleFromAEList(&theNumber, msg, reply);
  913.     if( aeErr )
  914.     {
  915.         return( aeErr );
  916.     }
  917.  
  918.     result = cos(theNumber);
  919.  
  920.     aeErr = AEPutParamPtr( &reply, kVUAESrvcResults, typeExtended,
  921.                             (Ptr) &result, sizeof( result ) );
  922.     if( aeErr )
  923.     {
  924.         ReportError( aeErr, 19 );
  925.         return( aeErr );
  926.     }
  927.  
  928.     return( aeErr );
  929. }
  930.  
  931. // The VUTanService will carry out the request to do
  932. //    tangent operation on a given number.
  933. OSErr  VUTanService( AppleEvent msg, AppleEvent reply, long )
  934. {
  935.     OSErr        aeErr;
  936.     extended    theNumber, result;
  937.     
  938.     
  939.     aeErr = ExtractExtendedSingleFromAEList(&theNumber, msg, reply);
  940.     if( aeErr )
  941.     {
  942.         return( aeErr );
  943.     }
  944.  
  945.     result = tan(theNumber);
  946.  
  947.     aeErr = AEPutParamPtr( &reply, kVUAESrvcResults, typeExtended,
  948.                             (Ptr) &result, sizeof( result ) );
  949.     if( aeErr )
  950.     {
  951.         ReportError( aeErr, 19 );
  952.         return( aeErr );
  953.     }
  954.  
  955.     return( aeErr );
  956. }
  957.  
  958. // The VUASinService will carry out the request to do
  959. //    arcsine operation on a given number.
  960. OSErr  VUASinService( AppleEvent msg, AppleEvent reply, long )
  961. {
  962.     OSErr        aeErr;
  963.     extended    theNumber, result;
  964.     
  965.     
  966.     aeErr = ExtractExtendedSingleFromAEList(&theNumber, msg, reply);
  967.     if( aeErr )
  968.     {
  969.         return( aeErr );
  970.     }
  971.  
  972.     result = asin(theNumber);
  973.  
  974.     aeErr = AEPutParamPtr( &reply, kVUAESrvcResults, typeExtended,
  975.                             (Ptr) &result, sizeof( result ) );
  976.     if( aeErr )
  977.     {
  978.         ReportError( aeErr, 19 );
  979.         return( aeErr );
  980.     }
  981.  
  982.     return( aeErr );
  983. }
  984.  
  985. // The VUACosService will carry out the request to do
  986. //    arccosine operation on a given number.
  987. OSErr  VUACosService( AppleEvent msg, AppleEvent reply, long )
  988. {
  989.     OSErr        aeErr;
  990.     extended    theNumber, result;
  991.  
  992.     
  993.     aeErr = ExtractExtendedSingleFromAEList(&theNumber, msg, reply);
  994.     if( aeErr )
  995.     {
  996.         return( aeErr );
  997.     }
  998.  
  999.     result = acos(theNumber);
  1000.  
  1001.     aeErr = AEPutParamPtr( &reply, kVUAESrvcResults, typeExtended,
  1002.                             (Ptr) &result, sizeof( result ) );
  1003.     if( aeErr )
  1004.     {
  1005.         ReportError( aeErr, 19 );
  1006.         return( aeErr );
  1007.     }
  1008.  
  1009.     return( aeErr );
  1010. }
  1011.  
  1012. // The VUATanService will carry out the request to do
  1013. //    arctangent operation on a given number.
  1014. OSErr  VUATanService( AppleEvent msg, AppleEvent reply, long )
  1015. {
  1016.     OSErr        aeErr;
  1017.     extended    theNumber, result;
  1018.     
  1019.     
  1020.     aeErr = ExtractExtendedSingleFromAEList(&theNumber, msg, reply);
  1021.     if( aeErr )
  1022.     {
  1023.         return( aeErr );
  1024.     }
  1025.  
  1026.     result = atan(theNumber);
  1027.  
  1028.     aeErr = AEPutParamPtr( &reply, kVUAESrvcResults, typeExtended,
  1029.                             (Ptr) &result, sizeof( result ) );
  1030.     if( aeErr )
  1031.     {
  1032.         ReportError( aeErr, 19 );
  1033.         return( aeErr );
  1034.     }
  1035.  
  1036.     return( aeErr );
  1037. }
  1038.  
  1039. // The VUSqrtService will carry out the request to do
  1040. //    a square root on a given number.
  1041. OSErr  VUSqrtService( AppleEvent msg, AppleEvent reply, long )
  1042. {
  1043.     OSErr        aeErr;
  1044.     extended    theNumber, result;
  1045.     
  1046.     
  1047.     aeErr = ExtractExtendedSingleFromAEList(&theNumber, msg, reply);
  1048.     if( aeErr )
  1049.     {
  1050.         return( aeErr );
  1051.     }
  1052.  
  1053.     result = sqrt(theNumber);
  1054.  
  1055.     aeErr = AEPutParamPtr( &reply, kVUAESrvcResults, typeExtended,
  1056.                             (Ptr) &result, sizeof( result ) );
  1057.     if( aeErr )
  1058.     {
  1059.         ReportError( aeErr, 19 );
  1060.         return( aeErr );
  1061.     }
  1062.  
  1063.     return( aeErr );
  1064. }
  1065.  
  1066. // VUPowerService will carry out the request to do 
  1067. //    a power operation with two numbers.
  1068. OSErr  VUPowerService( AppleEvent msg, AppleEvent reply, long )
  1069. {
  1070.     OSErr        aeErr;
  1071.     extended    theNumber, thePower, result;
  1072.     
  1073.     
  1074.     aeErr = ExtractExtendedPairFromAEList(&theNumber, &thePower, msg, reply);
  1075.     if( aeErr )
  1076.     {
  1077.         return( aeErr );
  1078.     }
  1079.  
  1080.     result = pow(theNumber, thePower);
  1081.  
  1082.     aeErr = AEPutParamPtr( &reply, kVUAESrvcResults, typeExtended,
  1083.                             (Ptr) &result, sizeof( result ) );
  1084.     if( aeErr )
  1085.     {
  1086.         ReportError( aeErr, 19 );
  1087.         return( aeErr );
  1088.     }
  1089.  
  1090.     return( aeErr );
  1091. }
  1092.  
  1093. // The VULogService will carry out the request to do
  1094. //    natural logarithm operation on a given number.
  1095. OSErr  VULogService( AppleEvent msg, AppleEvent reply, long )
  1096. {
  1097.     OSErr        aeErr;
  1098.     extended    theNumber, result;
  1099.     
  1100.     
  1101.     aeErr = ExtractExtendedSingleFromAEList(&theNumber, msg, reply);
  1102.     if( aeErr )
  1103.     {
  1104.         return( aeErr );
  1105.     }
  1106.  
  1107.     result = log(theNumber);
  1108.  
  1109.     aeErr = AEPutParamPtr( &reply, kVUAESrvcResults, typeExtended,
  1110.                             (Ptr) &result, sizeof( result ) );
  1111.     if( aeErr )
  1112.     {
  1113.         ReportError( aeErr, 19 );
  1114.         return( aeErr );
  1115.     }
  1116.  
  1117.     return( aeErr );
  1118. }
  1119.  
  1120. // VULongPlusService will carry out the request to do 
  1121. //    addition with long integers.
  1122. OSErr  VULongPlusService( AppleEvent msg, AppleEvent reply, long )
  1123. {
  1124.     OSErr        aeErr;
  1125.     long        leftNumber, rightNumber, result;
  1126.     
  1127.     
  1128.     aeErr = ExtractLongIntegerPairFromAEList(&leftNumber, &rightNumber, msg, reply);
  1129.     if( aeErr )
  1130.     {
  1131.         if( aeErr = VUErrorReply( &reply,
  1132.                                 "One of the parameters is not a number!",
  1133.                                 kNotANumber ) );
  1134.         return( aeErr );
  1135.     }
  1136.  
  1137.     result = leftNumber + rightNumber;
  1138.  
  1139.     aeErr = AEPutParamPtr( &reply, kVUAESrvcResults, typeLongInteger,
  1140.                             (Ptr) &result, sizeof( result ) );
  1141.     if( aeErr )
  1142.     {
  1143.         ReportError( aeErr, 19 );
  1144.         return( aeErr );
  1145.     }
  1146.  
  1147.     return( aeErr );
  1148. }
  1149.  
  1150. // VULongMinusService will carry out the request to do
  1151. //    subtraction with long integers.
  1152. OSErr  VULongMinusService( AppleEvent msg, AppleEvent reply, long )
  1153. {
  1154.     OSErr        aeErr;
  1155.     long        leftNumber, rightNumber, result;
  1156.     
  1157.     
  1158.     aeErr = ExtractLongIntegerPairFromAEList(&leftNumber, &rightNumber, msg, reply);
  1159.     if( aeErr )
  1160.     {
  1161.         return( aeErr );
  1162.     }
  1163.  
  1164.     result = leftNumber - rightNumber;
  1165.  
  1166.     aeErr = AEPutParamPtr( &reply, kVUAESrvcResults, typeLongInteger,
  1167.                             (Ptr) &result, sizeof( result ) );
  1168.     if( aeErr )
  1169.     {
  1170.         ReportError( aeErr, 19 );
  1171.         return( aeErr );
  1172.     }
  1173.  
  1174.     return( aeErr );
  1175. }
  1176.  
  1177. // The VULongTimesService will carry out the request to do
  1178. //    multiplication on long integers.
  1179. OSErr  VULongTimesService( AppleEvent msg, AppleEvent reply, long )
  1180. {
  1181.     OSErr        aeErr;
  1182.     long         leftNumber, rightNumber, result;
  1183.     
  1184.     
  1185.     aeErr = ExtractLongIntegerPairFromAEList(&leftNumber, &rightNumber, msg, reply);
  1186.     if( aeErr )
  1187.     {
  1188.         return( aeErr );
  1189.     }
  1190.  
  1191.     result = leftNumber * rightNumber;
  1192.  
  1193.     aeErr = AEPutParamPtr( &reply, kVUAESrvcResults, typeLongInteger,
  1194.                             (Ptr) &result, sizeof( result ) );
  1195.     if( aeErr )
  1196.     {
  1197.         ReportError( aeErr, 19 );
  1198.         return( aeErr );
  1199.     }
  1200.  
  1201.     return( aeErr );
  1202. }
  1203.  
  1204. // The VULongDivideService will carry out the request to do
  1205. //    division on long integers.
  1206. OSErr  VULongDivideService( AppleEvent msg, AppleEvent reply, long )
  1207. {
  1208.     OSErr        aeErr;
  1209.     long        leftNumber, rightNumber, result;
  1210.     
  1211.     
  1212.     aeErr = ExtractLongIntegerPairFromAEList(&leftNumber, &rightNumber, msg, reply);
  1213.     if( aeErr )
  1214.     {
  1215.         return( aeErr );
  1216.     }
  1217.  
  1218.     result = leftNumber  / rightNumber;
  1219.  
  1220.     aeErr = AEPutParamPtr( &reply, kVUAESrvcResults, typeLongInteger,
  1221.                             (Ptr) &result, sizeof( result ) );
  1222.     if( aeErr )
  1223.     {
  1224.         ReportError( aeErr, 19 );
  1225.         return( aeErr );
  1226.     }
  1227.  
  1228.     return( aeErr );
  1229. }
  1230.  
  1231. // The VULongModService will carry out the request to do
  1232. //    mod on long integers.
  1233. OSErr  VULongModService( AppleEvent msg, AppleEvent reply, long )
  1234. {
  1235.     OSErr        aeErr;
  1236.     long        leftNumber, rightNumber, result;
  1237.     
  1238.     
  1239.     aeErr = ExtractLongIntegerPairFromAEList(&leftNumber, &rightNumber, msg, reply);
  1240.     if( aeErr )
  1241.     {
  1242.         return( aeErr );
  1243.     }
  1244.  
  1245.     result = leftNumber % rightNumber;
  1246.  
  1247.     aeErr = AEPutParamPtr( &reply, kVUAESrvcResults, typeLongInteger,
  1248.                             (Ptr) &result, sizeof( result ) );
  1249.     if( aeErr )
  1250.     {
  1251.         ReportError( aeErr, 19 );
  1252.         return( aeErr );
  1253.     }
  1254.  
  1255.     return( aeErr );
  1256. }
  1257.  
  1258. // The VULongCompareService will carry out the request to do
  1259. //    comparison on long integers.
  1260. OSErr  VULongCompareService( AppleEvent msg, AppleEvent reply, long )
  1261. {
  1262.     OSErr        aeErr;
  1263.     long        leftNumber, rightNumber;
  1264.     short        result;
  1265.     
  1266.     
  1267.     aeErr = ExtractLongIntegerPairFromAEList(&leftNumber, &rightNumber, msg, reply);
  1268.     if( aeErr )
  1269.     {
  1270.         return( aeErr );
  1271.     }
  1272.  
  1273.     if (leftNumber > rightNumber)
  1274.         result = kGREATER;
  1275.     else if (leftNumber < rightNumber)
  1276.         result = kLESS;
  1277.     else
  1278.         result = kEQUAL;
  1279.         
  1280.     aeErr = AEPutParamPtr( &reply, kVUAESrvcResults, typeShortInteger,
  1281.                             (Ptr) &result, sizeof( result ) );
  1282.     if( aeErr )
  1283.     {
  1284.         ReportError( aeErr, 19 );
  1285.         return( aeErr );
  1286.     }
  1287.  
  1288.     return( aeErr );
  1289. }
  1290.  
  1291.  
  1292. pascal    OSErr  AEOpenHandler(AppleEvent, AppleEvent, long)
  1293. {
  1294. //    Standard (empty) handler for the 'oapp' Apple Event. Our program does
  1295. //    not need anything special here, so "noErr" can simply be returned.
  1296.  
  1297.     return(noErr);
  1298. }
  1299.  
  1300.  
  1301. pascal    OSErr  AEOpenDocHandler(AppleEvent, AppleEvent, long)
  1302. {
  1303. //    Standard (empty) handler for the 'odoc' Apple Event. Our program does
  1304. //    not have documents, so we ignore this Apple Event.
  1305.  
  1306.     return(noErr);
  1307. }
  1308.  
  1309.  
  1310. pascal    OSErr  AEQuitHandler(AppleEvent, AppleEvent, long)
  1311. {
  1312. //    Standard handler for the 'quit' Apple Event. You must never, ever call
  1313. //    ExitToShell from within an Apple Event handler. It is certain death
  1314. //    for your application. Thus, we just set a flag which is examined later
  1315. //    in the main event loop.
  1316.  
  1317.     quitting = TRUE;
  1318.     return(noErr);
  1319. }
  1320.  
  1321.  
  1322. pascal    OSErr  AEPrintHandler(AppleEvent, AppleEvent, long)
  1323. {
  1324. //    Standard (empty) handler for the 'pdoc' Apple Event. Our program does
  1325. //    not have documents, so we ignore this Apple Event.
  1326.  
  1327.     return(noErr);
  1328. }
  1329.  
  1330.  
  1331. void     ReportError(OSErr err, long where)
  1332. {
  1333. //    Reports an error by way of an Alert dialog. "err" is the error
  1334. //    code. "where" is an arbitrary (but unique) number indicating
  1335. //    where in the program the error occurred. Of course, if there
  1336. //    is no error, this routine does nothing.
  1337.  
  1338.     int        dontCare;
  1339.     Str255    errStr, whereStr;
  1340.  
  1341.     if (err != 0) {
  1342.         numtostring(err, errStr);
  1343.         numtostring(where, whereStr);
  1344.         paramtext(errStr, whereStr, '', '');
  1345.         dontCare = Alert(128, NIL);
  1346.     }
  1347. }
  1348.  
  1349.  
  1350. void    SrvcError(Str255 msg)
  1351. {
  1352. //    Displays an error dialog when the service name sent by V.U. 2.0
  1353. //    does not conform to a legal service understood by the external
  1354. //    tool. In reality, this program should check the Apple Event
  1355. //    user interaction level to determine how (and whether) to interact
  1356. //    with the user.
  1357.  
  1358.     int        dontCare;
  1359.  
  1360.     paramtext(msg, '', '', '');
  1361.     dontCare = Alert(128, NIL);
  1362. }
  1363.  
  1364. pascal  OSErr  VUErrorReply(AppleEvent *reply, Str255 errorText, OSErr errorNo)
  1365. {
  1366.     OSErr        aeErr;
  1367.  
  1368.     aeErr = AEPutParamPtr(reply,
  1369.                            keyErrorString,
  1370.                            typeChar,
  1371.                            errorText,
  1372.                            strlen(errorText));
  1373.     if (aeErr == noErr) {
  1374.         aeErr = AEPutParamPtr(reply,
  1375.                                keyErrorNumber,
  1376.                                typeShortInteger,
  1377.                                (Ptr)&errorNo,
  1378.                                sizeof(errorNo));
  1379.         }
  1380.     return(aeErr);
  1381. }
  1382.  
  1383.  
  1384. void     InitAEStuff(void)
  1385. {
  1386. //    Initialization of the Apple Event Manager and dispatch table.
  1387. //    If we don't have Apple Events or installation of any of the
  1388. //    Apple Event handlers fails, the program is simply terminated.
  1389. //    The routine starts by installing standard handlers for the
  1390. //    core Apple Events. This is followed by installation of the
  1391. //    event handlers for the V.U. 2.0 external tool interface.
  1392.  
  1393.     long            response;
  1394.     OSErr            aeErr;
  1395.  
  1396.     hasAE = (Gestalt(gestaltAppleEventsAttr, &response) == noErr);
  1397.     if (hasAE) {    // Has AE
  1398.         aeErr = AEInstallEventHandler(kVUAETool,
  1399.                                        kVUAESendService,
  1400.                                        (AEEventHandlerProcPtr) VUServiceHandler,
  1401.                                        0, FALSE);
  1402.         if (aeErr != noErr)  ExitToShell();
  1403.         aeErr = AEInstallEventHandler(kCoreEventClass,
  1404.                                        kAEOpenApplication,
  1405.                                        (AEEventHandlerProcPtr) AEOpenHandler,
  1406.                                        0, FALSE);
  1407.         if (aeErr != noErr) ExitToShell();
  1408.         aeErr = AEInstallEventHandler(kCoreEventClass,
  1409.                                        kAEOpenDocuments,
  1410.                                        (AEEventHandlerProcPtr) AEOpenDocHandler,
  1411.                                        0, FALSE);
  1412.         if (aeErr != noErr) ExitToShell();
  1413.         aeErr = AEInstallEventHandler(kCoreEventClass,
  1414.                                        kAEQuitApplication,
  1415.                                        (AEEventHandlerProcPtr) AEQuitHandler,
  1416.                                        0, FALSE);
  1417.         if (aeErr != noErr) ExitToShell();
  1418.         aeErr = AEInstallEventHandler(kCoreEventClass,
  1419.                                        kAEPrintDocuments,
  1420.                                        (AEEventHandlerProcPtr) AEPrintHandler,
  1421.                                        0, FALSE);
  1422.         if (aeErr != noErr) ExitToShell();
  1423.         aeErr = AESetInteractionAllowed(kAEInteractWithAll);
  1424.         if (aeErr != noErr)  ExitToShell();
  1425.     }
  1426.     else
  1427.         ExitToShell();
  1428. }
  1429.  
  1430.  
  1431. void     BuildMenuBar(void)
  1432. {
  1433. //    Construct a menu bar for our application. (Yawn)
  1434.  
  1435.     theMenuBar = GetNewMBar(kMBarID);
  1436.     SetMenuBar(theMenuBar);
  1437.     appleMenu = GetMHandle(kAppleMenu);
  1438.     fileMenu = GetMHandle(kFileMenu);
  1439.     editMenu = GetMHandle(kEditMenu);
  1440.     AddResMenu(appleMenu, 'DRVR');
  1441.     DrawMenuBar();
  1442. }
  1443.  
  1444.  
  1445. void     InitProgram(void)
  1446. {
  1447. //    Initialize the Macintoshâ„¢ Toolbox and application
  1448. //    environment.
  1449.  
  1450.     quitting = FALSE;
  1451.     background = FALSE;
  1452.     MaxApplZone();
  1453.     InitGraf(&qd.thePort);
  1454.     InitFonts();
  1455.     InitWindows();
  1456.     InitMenus();
  1457.     TEInit();
  1458.     InitDialogs(NIL);
  1459.     InitCursor();
  1460.     
  1461.     InitAEStuff();
  1462.     BuildMenuBar();
  1463. }
  1464.  
  1465.  
  1466. void     Idling(void)
  1467. {
  1468. //    We don't do anything while idling. Perhaps we should sing a song?
  1469. }
  1470.  
  1471.  
  1472. void     InvokeDA(MenuHandle theMenu, long theItem)
  1473. {
  1474. //    Bring up a desk accessory.
  1475.  
  1476.     OSErr            err;
  1477.     Str255            da;
  1478.  
  1479.     GetItem(theMenu, theItem, da);
  1480.     err = OpenDeskAcc(da);
  1481. }
  1482.  
  1483.  
  1484. void     MenuCommand(long theCmd)
  1485. {
  1486. //    Activate the appropriate menu item.
  1487.  
  1488.     long            menuID, menuItem;
  1489.     int                dontCare;
  1490.  
  1491.     menuID = HiWord(theCmd);
  1492.     menuItem = LoWord(theCmd);
  1493.     switch (menuID ) {
  1494.         case kAppleMenu:
  1495.             if (menuItem != 1)  InvokeDA(appleMenu, menuItem);
  1496.             else dontCare = Alert(666, NIL);
  1497.             break;
  1498.         case kFileMenu:
  1499.             quitting = TRUE;
  1500.             break;
  1501.         case kEditMenu: break;
  1502.     }
  1503.     HiliteMenu(0);
  1504. }
  1505.  
  1506.  
  1507. void     DispatchMouseDown(EventRecord theEvent)
  1508. {
  1509. //    Dispatch a mouse down event to the proper handler.
  1510.  
  1511.     WindowPtr            theWindow;
  1512.  
  1513.     switch (FindWindow(theEvent.where, &theWindow)) {
  1514.         case inMenuBar:
  1515.             MenuCommand(MenuSelect(theEvent.where));
  1516.             break;
  1517.         case inSysWindow:
  1518.             SystemClick(&theEvent, theWindow);
  1519.             break;
  1520.         case inDrag:
  1521.             if (theWindow == FrontWindow())
  1522.                 DragWindow(theWindow, theEvent.where, &qd.screenBits.bounds);
  1523.             break;
  1524.         case inContent: case inDesk: case inGrow: case inGoAway: 
  1525.             break;
  1526.     }
  1527. }
  1528.  
  1529.  
  1530. void     DispatchKeyDown(EventRecord theEvent)
  1531. {
  1532. //    Dispatch a key down event to the proper handler. Since typing
  1533. //    isn't supported, the routine only checks for service key equivalents.
  1534.  
  1535.     char            key;
  1536.  
  1537.     key = theEvent.message & charCodeMask;
  1538.     if ((theEvent.modifiers & cmdKey) && (theEvent.what == keyDown))
  1539.         MenuCommand(MenuKey(key));
  1540. }
  1541.  
  1542.  
  1543. void     ActivateWindow(WindowPtr, Boolean)
  1544. {
  1545. //    Gee, we don't have any windows worth activating.
  1546. }
  1547.  
  1548.  
  1549. void     UpdateWindow(WindowPtr)
  1550. {
  1551. //    Gee, we don't have any windows worth updating.
  1552. }
  1553.  
  1554. void     DiskInserted(long diskInfo)
  1555. {
  1556. //    Handle disk insert errors.
  1557.  
  1558.     Point        where;
  1559.     OSErr        dontCare;
  1560.  
  1561.     if (HiWord(diskInfo) != noErr) {
  1562.         SetPt(&where, 40, 40);
  1563.         dontCare = DIBadMount(where, diskInfo);
  1564.     }
  1565. }
  1566.  
  1567.  
  1568. void     DispatchOSEvent(EventRecord theEvent)
  1569. {
  1570. //    Dispatch OS events.
  1571.  
  1572.     switch ((theEvent.message >> 24) & 0x0FF) {
  1573.         case kMouseMovedMessage:
  1574.             Idling();
  1575.             break;
  1576.         
  1577.         case kSuspendResumeMessage:
  1578.             background = (theEvent.message & resumeFlag);
  1579.             ActivateWindow(FrontWindow(), !background);
  1580.             break;
  1581.     }
  1582. }
  1583.  
  1584.  
  1585. void     DispatchHighEvent(EventRecord theEvent)
  1586. {
  1587. //    Send off those Apple Events!
  1588.  
  1589.     aeError = AEProcessAppleEvent(&theEvent);
  1590. }
  1591.